package com.csxy.systemmanager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.systemmanager.entity.Good;
import com.csxy.systemmanager.entity.IdList;
import com.csxy.systemmanager.common.ResponseResult;
import com.csxy.systemmanager.entity.Supplier;
import com.csxy.systemmanager.service.GoodService;
import com.csxy.systemmanager.service.SupplierService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Good)表控制层
 *
 * @author makejava
 * @since 2024-06-15 15:15:48
 */
@RestController
@RequestMapping("/supplier")
@CrossOrigin
public class SupplierController {
    /**
     * 服务对象
     */
    @Resource
    private SupplierService supplierService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页索引
     * @param size    页大小
     *
     * @return 所有数据
     */
    @GetMapping("/list")
    public ResponseResult selectAll(Integer current, Integer size, String query) {
        Page<Supplier> page = supplierService.listByPage(current, size, query);
        return new ResponseResult(200, "ok", page);
    }

    /**
     * 新增数据
     *
     * @param supplier 实体对象
     * @return 新增结果
     */
    @PutMapping
    public ResponseResult insert(@RequestBody Supplier supplier) {
        boolean save = supplierService.save(supplier);
        if (save) {
            return new ResponseResult(200, "ok", null);
        } else {
            return new ResponseResult(500, "error", null);
        }
    }

    /**
     * 修改数据
     *
     * @param supplier 实体对象
     * @return 修改结果
     */
    @PostMapping
    public ResponseResult update(@RequestBody Supplier supplier) {
        boolean update = supplierService.updateById(supplier);
        if (update) {
            return new ResponseResult(200, "ok", null);
        } else {
            return new ResponseResult(500, "error", null);
        }
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public ResponseResult delete(@RequestBody IdList idList) {
        boolean remove = supplierService.removeByIds(idList.getIdList());
        if (remove) {
            return new ResponseResult(200, "ok", null);
        } else {
            return new ResponseResult(500, "error", null);
        }
    }


    @GetMapping("/search")
    public List<Supplier> findByConditions(@RequestParam String supplierName) {
        return supplierService.findByConditions(supplierName);
    }
}

